//
//  JinrituijianViewController.m
//  ThePrivateKitchen
//
//  Created by lirui on 16/1/24.
//  Copyright © 2016年 www.lanou3g.com蓝鸥科技. All rights reserved.
//

#import "JinrituijianViewController.h"

@interface JinrituijianViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)NSMutableArray *dataArr;
@property(nonatomic, assign)BOOL isRefrsh;
@property(nonatomic, assign)NSInteger index;
@end

//static NSInteger i = 1;
//static NSInteger j = 0;
@implementation JinrituijianViewController
- (void)dealloc
{
    [_tableView release];
    [_dataArr release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [ UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.title = @"人气专题";
    self.navigationController.navigationBar.translucent = NO;
    //数组初始化
    self.dataArr = [ NSMutableArray array];
    [self creatView];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"left@3x"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(leftAction:)];
    
}
-(void)leftAction:(UIBarButtonItem *)left
{
    [self.navigationController popViewControllerAnimated:YES];
}

   
  

#pragma mark -创建tableview
-(void)creatView
{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH *375, HEIGHT *667- 49) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    [_tableView release];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    //    self.tableView.contentInset = UIEdgeInsetsMake(-64, 0, 0, 0);
    [self.tableView registerClass:[Head_2TableViewCell class] forCellReuseIdentifier:@"cell"];
    
    //下拉刷新
   MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
       //赋初值
     self.index = 1;
        
        self.isRefrsh = YES;
        
        [self prepareData];
    }];
    self.tableView.mj_header = header;
    [self.tableView.mj_header beginRefreshing];
    //上拉加载
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            self.index ++;
        self.isRefrsh = NO;
        [self prepareData];
    }];
    self.tableView.mj_footer = footer;


}
//数据请求
-(void)prepareData
{
    NSString *str = [NSString stringWithFormat:@"http://api.meishi.cc/v5/caidans_list.php?format=json?lat=38.88272632413187&lon=121.5392187104746&source=iphone&format=json&t=3&page=%ld",(long)self.index];
    NSLog(@"~~~~~~~~%ld",(long)self.index);
    [LRAFNetwork postUrlString:str body:nil response:JSON bodyStyle:RequestJSON requestHeadFile:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        //如果是刷新状态
        if (self.isRefrsh ) {
          
            [self.dataArr removeAllObjects];
        }
        NSArray *arr = responseObject[@"data"];
       
        [self.dataArr addObjectsFromArray:[TopicModel modelHanderWithArray:arr]];
        if (responseObject) {
           NSLog(@"%@",_dataArr);
          [self.tableView reloadData];
       }
        
        [self.tableView.mj_header endRefreshing];
         [self.tableView.mj_footer endRefreshing];
        
           } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
    
   
   

}
#pragma mark -必须实现的洗衣方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   Head_2TableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    TopicModel *m = _dataArr[indexPath.row];
    [cell.img sd_setImageWithURL:[NSURL URLWithString:m.photo]];
    cell.name.text = m.title;
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return HEIGHT * 200;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    TuijianViewController *tuijian =[[ TuijianViewController alloc] init];
    tuijian.string = [_dataArr[indexPath.row] f_s_type];
    [self.navigationController pushViewController:tuijian animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
